/*
 * 文件名：CellBuildingService.java 版权：Copyright by www.huawei.com 描述： 修改人：Administrator 修改时间：2016年4月7日
 * 跟踪单号： 修改单号： 修改内容：
 */

package com.zcsy.manage.service.base.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zcsy.commons.constants.Constants;
import com.zcsy.manage.model.base.CellBuilding;
import com.zcsy.manage.persistence.base.CellBuildingDao;
import com.zcsy.manage.service.base.ICellBuildingService;
import com.zcsy.manage.service.base.ICellHouseService;
import com.zcsy.manage.service.base.ICellUnitService;
import com.zcsy.manage.service.system.impl.SystemComService;


/**
 * @Title: CellBuildingService.java
 * @Package： com.zcsy.manage.service.base.impl
 * @Description: 小区楼栋service层接口实现类
 * @author lilan 1063972142@qq.com
 * @Copyright: 2015 武汉恒数利通信息技术有限公司 All rights reserved.
 * @date 2016年4月7日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Service
public class CellBuildingService extends SystemComService implements ICellBuildingService {

    @Autowired
    private CellBuildingDao cellBuildingDao;

	@Autowired
	private ICellUnitService cellUnitService;
	
	@Autowired
	private ICellHouseService cellHouseService;
	
    @Override
    public int insert(CellBuilding cellBuilding) {
        return cellBuildingDao.insert(cellBuilding);
    }

    @Override
    public int update(CellBuilding cellBuilding) {
    	CellBuilding oldInfo = cellBuildingDao.selectByPrimaryKey(cellBuilding.getId());

    	//如果楼栋名称修改了，则修改该楼栋下的所有单元和所有房屋的楼栋名
        if(!oldInfo.getBuildingName().equals(cellBuilding.getBuildingName())){
            cellUnitService.updateById(oldInfo.getId(), cellBuilding.getBuildingName(), Constants.BUILDING_TYPE);
            cellHouseService.updateById(oldInfo.getId(), cellBuilding.getBuildingName(), Constants.BUILDING_TYPE);
        }
    	
        oldInfo.setBuildingName(cellBuilding.getBuildingName());
        oldInfo.setUserAccount(cellBuilding.getUserAccount());
        oldInfo.setSort(cellBuilding.getSort());
        oldInfo.setMemo(cellBuilding.getMemo());
        oldInfo.setUpdateTime(cellBuilding.getUpdateTime());
        oldInfo.setRegionId(cellBuilding.getRegionId());
        oldInfo.setRegionName(cellBuilding.getRegionName());
        
        return cellBuildingDao.update(oldInfo);
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return cellBuildingDao.deleteByPrimaryKey(id);
    }

    @Override
    public CellBuilding selectByPrimaryKey(String id) {
        return cellBuildingDao.selectByPrimaryKey(id);
    }

    @Override
    public List<CellBuilding> selectByKeyword(CellBuilding cellBuilding) {
        return cellBuildingDao.selectByKeyword(cellBuilding);
    }

    @Override
    public List<CellBuilding> getCellBuildingByRegionId(String regionId) {
        return cellBuildingDao.getCellBuildingByRegionId(regionId);
    }

    @Override
    public int updateById(String id, String name, int type) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("name", name);
        map.put("type", type);
        return cellBuildingDao.updateById(map);
    }

    /*
     * (non-Javadoc)
     * @see
     * com.zcsy.manage.service.base.ICellBuildingService#selectForImport(com.zcsy.manage.model.
     * base.CellBuilding)
     */
    @Override
    public List<CellBuilding> selectForImport(CellBuilding record) {
        return cellBuildingDao.selectForImport(record);
    }

	@Override
	public List<CellBuilding> getBuildingAndUnitAndHouse(String regionId) {
		return cellBuildingDao.getBuildingAndUnit(regionId);
	}

	@Override
	public int updateSort(CellBuilding record) {
		return cellBuildingDao.updateSort(record);
	}

    @Override
    public boolean isNameExists(String buildId, String regionId, String name) {
        return cellBuildingDao.isNameExists(buildId, regionId, name);
    }
}
